package com.autonavi.abs.common.aop;

import com.autonavi.abs.common.api.ServiceResponse;
import com.autonavi.abs.common.enumeration.ResponseCodeEnum;
import com.autonavi.abs.common.exception.MessageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author wwy
 * @version V1.0
 * <p>Description: 异常处理器</p>
 * @date 2019-10-30 19:20
 */
@ControllerAdvice
public class ControllerHandler {

    private final static Logger LOGGER = LoggerFactory.getLogger(ControllerHandler.class);

    /**
     * messageException可以把异常信息透出
     *
     * @param messageException
     * @return
     */
    @ExceptionHandler(value = {MessageException.class})
    @ResponseBody
    public ServiceResponse handle(MessageException messageException) {
        LOGGER.error(messageException.getMessage(), messageException);
        return ServiceResponse.fail(ResponseCodeEnum.INTERFACE_EXCEPTION.getCode(), messageException.getMessage());
    }

    /**
     * 只返回失败，不返回具体异常信息
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public ServiceResponse handle(Exception exception) {
        LOGGER.error(exception.getMessage(), exception);
        return ServiceResponse.fail(ResponseCodeEnum.INTERFACE_EXCEPTION);
    }

}
